iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 16
0
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 16

Day16 - Codewars 刷題

  • 分享至 

  • xImage
  •  

昨天 Codewars LV5 題目思考方向錯誤
導致特定情境下看起來是對的
實際上是有問題的
換個方向思考答案跟著來


題目(Double Cola)

Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on.

For example, Penny drinks the third can of cola and the queue will look like this:

Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny
Write a program that will return the name of the person who will drink the n-th cola.

Input
The input data consist of an array which contains at least 1 name, and single integer n which may go as high as the biggest number your language of choice supports (if there's such limit, of course).

Output / Examples
Return the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1.

whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 1) == "Sheldon"
whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 52) == "Penny"
whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 7230702951) == "Leonard"
def who_is_next(names, r)
  #your code here
end

names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]

Test.assert_equals(who_is_next(names, 1), "Sheldon")
Test.assert_equals(who_is_next(names, 52), "Penny")
Test.assert_equals(who_is_next(names, 7230702951), "Leonard")

題目(You're a square!)

A square of squares
You like building blocks. You especially like building blocks that are squares. And what you even like more, is to arrange them into a square of square building blocks!

However, sometimes, you can't arrange them into a square. Instead, you end up with an ordinary rectangle! Those blasted things! If you just had a way to know, whether you're currently working in vain… Wait! That's it! You just have to check if your number of building blocks is a perfect square.
def is_square(x)
  false
end

describe "is_square" do
  it "should work for some examples" do
    Test.assert_equals (is_square (-1)), false, "-1 is not a perfect square"
    Test.assert_equals (is_square  0),   true,  "0 is a perfect square (0 * 0)"
    Test.assert_equals (is_square  3),   false,  "3 is not a perfect square"
    Test.assert_equals (is_square  4),    true,   "4 is a perfect square (2 * 2)"
    Test.assert_equals (is_square 25),    true,  "25 is a perfect square (5 * 5)"
    Test.assert_equals (is_square 26),   false, "26 is not a perfect square"
  end 
end

影片解題:
Yes


答案:

# Double Cola
def who_is_next(names, r)
  len = names.length
  while r > len
    r -= len
    len *= 2
  end
  names[(r-1)/(len/names.length)]
end


# You're a square!
def is_square(x)
  x < 0 ? false : Math.sqrt(x) % 1 == 0
end

本文同步發布於 小菜的 Blog https://riverye.com/


上一篇
Day15 - Codewars 刷題
下一篇
Day17 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言